package com.dg.cuckoo.admin.service.impl;

import com.dg.cuckoo.admin.dto.other.FileDto;
import com.dg.cuckoo.admin.dto.projectInfo.ProjectImageDto;
import com.dg.cuckoo.admin.repository.FileRepository;
import com.dg.cuckoo.admin.repository.ProjectImageRepository;
import com.dg.cuckoo.admin.service.ProjectImageService;
import com.dg.cuckoo.admin.service.ProjectInfoServer;
import com.dg.cuckoo.common.utils.PageModel;
import com.dg.cuckoo.core.ServiceException;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProjectImageServiceimpl implements ProjectImageService {

    @Autowired
    private ProjectImageRepository repository;
    @Autowired
    private ProjectInfoServer projectInfoServer;
    @Autowired
    private FileRepository fileRepository;

    @Override
    public void save(String projectId, ProjectImageDto dto, MultipartFile[] files) throws Exception {

        checkSave(dto);
        dto.setProjectId(projectId);
        ProjectImageDto save = repository.save(dto);
        if(files!=null){
            projectInfoServer.powerfulFileSave(files,save.getId());
        }

    }

    @Override
    public void delect(String id) {
        if(id==null){
            throw new ServiceException("请选择行");
        }
        repository.deleteById(id);
        List<FileDto> dtos = fileRepository.findByByMark(id);
        fileRepository.deleteAll(dtos);
    }

    @Override
    public PageModel powerfulPage(Integer page, Integer pageSize,String id) {
        Integer one = page;
        page = (page - 1) * pageSize;
        int size = repository.findAll().size();
        List<ProjectImageDto> list = repository.page(id,page, pageSize);
        PageModel model = new PageModel(one, pageSize, size, list);
        return model;
    }

    @Override
    public String openSerialInfo(String id) {
        if(id==null){
            throw new ServiceException("请选择行");
        }
        ProjectImageDto dto = repository.findById(id).get();
        List<FileDto> fileDtos = fileRepository.findByByMark(id);
        List<String> urls=new ArrayList<>();
        for(FileDto f:fileDtos){
            urls.add(f.getFileUrl());
        }
        JSONObject json = new JSONObject();
        json.put("dto",dto);
        json.put("urls",urls);
        return json.toString();
    }

    @Override
    public String openTextNumberInfo(String id) {
        if(id==null){
            throw new ServiceException("请选择行");
        }
        return repository.findById(id).get().getContentBox();
    }

    @Override
    public void updata(String id, String projectId, ProjectImageDto dto, MultipartFile[] files) throws Exception {
        if(id==null){
            throw new ServiceException("请选择行");
        }
        checkSave(dto);
        dto.setProjectId(projectId);
        ProjectImageDto save = repository.save(dto);
        if (files!=null){
            if(files.length>0){
                fileRepository.deleteAll(fileRepository.findByByMark(id));
                projectInfoServer.powerfulFileSave(files,save.getId());
        }
        }
    }


    public void checkSave(ProjectImageDto dto){

    }
}
